home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / CC_C / H001C.ZIP / DJGPP3.ZIP / INCLUDE / RNDINT.H < prev    next >
C/C++ Source or Header  |  1991-03-03  |  4KB  |  170 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1990 Free Software Foundation
  4.     adapted from a submission from John Reidl <riedl@cs.purdue.edu>
  5.  
  6.  
  7. GNU CC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU CC General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU CC, but only under the conditions described in the
  16. GNU CC General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU CC so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  
  21. */
  22.  
  23. #ifndef _RandomInteger_h
  24. #ifdef __GNUG__
  25. #pragma once
  26. #pragma interface
  27. #endif
  28. #define _RandomInteger_h 1
  29.  
  30. // RandomInteger uses a random number generator to generate an integer
  31. // in a specified range.  By default the range is 0..1.  Since in my
  32. // experience random numbers are often needed for a wide variety of
  33. // ranges in the same program, this generator accepts a new low or high value
  34. // as an argument to the asLong and operator() methods to temporarily 
  35. // override stored values
  36.  
  37. #include <math.h>
  38. #include "RNG.h"
  39.  
  40. class RandomInteger 
  41. {
  42.  protected:
  43.   RNG *pGenerator;
  44.   long pLow;
  45.   long pHigh;
  46.  
  47.   long _asLong(long, long);
  48.  
  49.  public:
  50.  
  51.        RandomInteger(long low, long high, RNG *gen);
  52.        RandomInteger(long high, RNG *gen);
  53.        RandomInteger(RNG *gen);
  54.  
  55. // read params
  56.  
  57.   long low() const;
  58.   long high() const;
  59.   RNG* generator() const;
  60.  
  61. // change params
  62.  
  63.   long low(long x);
  64.   long high(long x);
  65.   RNG* generator(RNG *gen);
  66.  
  67. // get a random number
  68.  
  69.   long asLong();
  70.   long operator()(); // synonym for asLong
  71.   int  asInt();      // (possibly) truncate as int
  72.  
  73. // override params for one shot
  74.  
  75.   long asLong(long high);
  76.   long asLong(long low, long high);
  77.  
  78.   long operator () (long high);  // synonyms
  79.   long operator () (long low, long high);
  80.  
  81. };
  82.  
  83. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  84.  
  85. inline RandomInteger::RandomInteger(long low, long high, RNG *gen) 
  86.      : pLow((low < high) ? low : high),
  87.        pHigh((low < high) ? high : low),
  88.        pGenerator(gen)
  89. {}
  90.  
  91. inline RandomInteger::RandomInteger(long high, RNG *gen) 
  92.      : pLow((0 < high) ? 0 : high),
  93.        pHigh((0 < high) ? high : 0),
  94.        pGenerator(gen)
  95. {}
  96.   
  97.  
  98. inline RandomInteger::RandomInteger(RNG *gen) 
  99.      : pLow(0),
  100.        pHigh(1),
  101.        pGenerator(gen)
  102. {}
  103.  
  104. inline RNG* RandomInteger::generator() const { return pGenerator;}
  105. inline long RandomInteger::low() const       { return pLow; }
  106. inline long RandomInteger::high() const      { return pHigh; }
  107.  
  108. inline RNG* RandomInteger::generator(RNG *gen) 
  109. {
  110.   RNG *tmp = pGenerator; pGenerator = gen;  return tmp;
  111. }
  112.  
  113. inline long RandomInteger::low(long x)  
  114. {
  115.   long tmp = pLow;  pLow = x;  return tmp;
  116. }
  117.  
  118. inline long RandomInteger:: high(long x) 
  119. {
  120.   long tmp = pHigh; pHigh = x; return tmp;
  121. }
  122.  
  123. inline long RandomInteger:: _asLong(long low, long high)
  124. {    
  125.   return (pGenerator->asLong() % (high-low+1)) + low;
  126. }
  127.  
  128.  
  129. inline long RandomInteger:: asLong() 
  130. {
  131.   return _asLong(pLow, pHigh);
  132. }
  133.  
  134. inline long RandomInteger:: asLong(long high)
  135. {
  136.   return _asLong(pLow, high);
  137. }
  138.  
  139. inline long RandomInteger:: asLong(long low, long high)
  140. {
  141.   return _asLong(low, high);
  142. }
  143.  
  144. inline long RandomInteger:: operator () () 
  145. {
  146.   return _asLong(pLow, pHigh);
  147. }
  148.  
  149. inline long RandomInteger:: operator () (long high)
  150. {
  151.   return _asLong(pLow, high);
  152. }
  153.  
  154. inline long RandomInteger:: operator () (long low, long high)
  155. {
  156.   return _asLong(low, high);
  157. }
  158.  
  159.  
  160.  
  161.  
  162. inline int RandomInteger:: asInt() 
  163. {
  164.   return int(asLong());
  165. }
  166.  
  167. #endif
  168.  
  169. #endif
  170.